package common_phrase

import (
	"errors"
	"hft/Database/admin/common_phrase"
	"hft/e"
	"hft/global"
)

// 常用语
type CommonPhraseCate struct{}

// 获取常用语分类
func (*CommonPhraseCate) List() []*common_phrase.CommonPhraseCate {
	var list = make([]*common_phrase.CommonPhraseCate, 0)
	global.OC_DB.Order("category_sort desc").Find(&list)
	return list
}

// 创建
func (*CommonPhraseCate) Add(data *common_phrase.CommonPhraseCate) (err error) {
	if err = global.OC_DB.Model(&common_phrase.CommonPhraseCate{}).Create(map[string]interface{}{
		"category_sort": data.CategorySort,
		"category_name": data.CategoryName,
		"can_edit":      data.CanEdit,
	}).Error; err != nil {
		return errors.New(e.ErrorInfo)
	}
	return nil
}

// 编辑
func (*CommonPhraseCate) Edit(data *common_phrase.CommonPhraseCate) (err error) {
	if res := global.OC_DB.Model(&common_phrase.CommonPhraseCate{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"category_sort": data.CategorySort,
		"category_name": data.CategoryName,
		"can_edit":      data.CanEdit,
	}); res.RowsAffected == 0 || res.Error != nil {
		return errors.New(e.ErrorInfo)
	}
	return nil
}

// 删除
func (*CommonPhraseCate) Delete(ids []int) (err error) {
	if err = global.OC_DB.Delete(&common_phrase.CommonPhraseCate{}, ids).Error; err != nil {
		return errors.New(e.ErrorInfo)
	}
	return nil
}

var CommonPhraseCateService = new(CommonPhraseCate)
